﻿package mortal.game.utils
{
    import Message.DB.Tables.*;
    import Message.Game.*;
    import Message.Public.*;
    import __AS3__.vec.*;
    import com.gengine.utils.*;
    import extend.language.*;
    import flash.utils.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.model.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.view.common.util.*;
    import mortal.game.view.petNew.*;
    import mortal.game.view.skill.*;

    public class PetUtil extends Object
    {
        public static const ATTRIBUTE_NAME:Array = ["attack", "physDefense", "magicDefense", "penetration", "jouk", "hit", "crit", "toughness", "block", "expertise"];
        public static const ATTRIBUTE_NAME2:Array = ["life", "attack", "physDefense", "magicDefense", "penetration", "jouk", "hit", "crit", "toughness", "block", "expertise"];
        public static const ATTRIBUTE_NAME_3:Array = ["attack", "penetration", "jouk", "hit", "crit", "toughness", "block", "expertise", "defense", "life"];
        private static var _mountGroupItem:ItemData = new ItemData(410520001);
        private static var _mountQualifiedItem:ItemData = new ItemData(410510001);
        public static var maxPetLevel:int = 10;

        public function PetUtil()
        {
            return;
        }// end function

        public static function getGrowLevel(param1:int) : int
        {
            if (param1 >= 40)
            {
                return 4;
            }
            if (param1 >= 32)
            {
                return 3;
            }
            if (param1 >= 24)
            {
                return 2;
            }
            if (param1 >= 16)
            {
                return 1;
            }
            return 0;
        }// end function

        public static function getTalentColor(param1:int) : ColorInfo
        {
            var _loc_2:String = null;
            _loc_2 = ColorConfig.instance.getItemColorString(1);
            var _loc_3:* = new ColorInfo();
            _loc_3.color = _loc_2;
            return _loc_3;
        }// end function

        public static function getTalentMax(param1:int) : int
        {
            return PetConfig.instance.getInfoByCode(param1).talentMax;
        }// end function

        public static function getNameHtmlText(param1:SPet) : String
        {
            return HTMLUtil.addColor(param1.publicPet.name, getTalentColor(param1.publicPet.talent).color);
        }// end function

        public static function getNameHtmlText2(param1:String, param2:int) : String
        {
            return HTMLUtil.addColor(param1, getTalentColor(param2).color);
        }// end function

        public static function isGoodPet(param1:SPet) : Boolean
        {
            return param1.publicPet.level >= 50 || param1.publicPet.talent >= 800;
        }// end function

        public static function isTalentSkill(param1:int) : Boolean
        {
            var _loc_2:* = param1.toString();
            var _loc_3:* = _loc_2.substr(0, 2);
            return _loc_3 == "30";
        }// end function

        public static function getMaxLife(param1:SPet) : int
        {
            var _loc_2:* = 1 + int(param1.addPercent["maxLifeAdd"]) * 1 / 10000;
            return (param1.baseFight["maxLife"] + param1.extraFight["maxLife"]) * _loc_2;
        }// end function

        public static function getNumNameBygrow(param1:int) : String
        {
            if (param1 == 16)
            {
                return "PetGrowGreenNum.png";
            }
            if (param1 == 24)
            {
                return "PetGrowBlueNum.png";
            }
            if (param1 == 32)
            {
                return "PetGrowPurpleNum.png";
            }
            return "PetGrowYellowNum.png";
        }// end function

        public static function getPetModel(param1:int, param2:int) : EntityModelInfo
        {
            var _loc_3:int = 1;
            if (param1 >= 40)
            {
                _loc_3 = 5;
            }
            else if (param1 >= 32)
            {
                _loc_3 = 4;
            }
            else if (param1 >= 24)
            {
                _loc_3 = 3;
            }
            else if (param1 >= 16)
            {
                _loc_3 = 2;
            }
            else
            {
                _loc_3 = 1;
            }
            return getModelInfoByLevel(_loc_3, param2);
        }// end function

        public static function getModelInfoByLevel(param1:int, param2:int) : EntityModelInfo
        {
            var _loc_3:* = ModelConfig.instance.getInfoByCode(param2);
            var _loc_4:* = new EntityModelInfo();
            _loc_4.proportion1 = _loc_3.proportion1;
            switch(param1)
            {
                case 1:
                {
                    _loc_4.bone = _loc_3.bone1;
                    _loc_4.texture = _loc_3.texture1;
                    _loc_4.mesh = _loc_3.mesh1;
                    _loc_4.effect = _loc_3.effect1;
                    _loc_4.skillEffect = _loc_3.skillEffect1;
                    setPetHoverArea(_loc_4, _loc_3.mouseArea1);
                    break;
                }
                case 2:
                {
                    _loc_4.bone = _loc_3.bone2;
                    _loc_4.texture = _loc_3.texture2;
                    _loc_4.mesh = _loc_3.mesh2;
                    _loc_4.effect = _loc_3.effect2;
                    _loc_4.skillEffect = _loc_3.skillEffect2;
                    setPetHoverArea(_loc_4, _loc_3.mouseArea2);
                    break;
                }
                case 3:
                {
                    _loc_4.bone = _loc_3.bone3;
                    _loc_4.texture = _loc_3.texture3;
                    _loc_4.mesh = _loc_3.mesh3;
                    _loc_4.effect = _loc_3.effect3;
                    _loc_4.skillEffect = _loc_3.skillEffect3;
                    setPetHoverArea(_loc_4, _loc_3.mouseArea3);
                    break;
                }
                case 4:
                {
                    _loc_4.bone = _loc_3.bone4;
                    _loc_4.texture = _loc_3.texture4;
                    _loc_4.mesh = _loc_3.mesh4;
                    _loc_4.effect = _loc_3.effect4;
                    _loc_4.skillEffect = _loc_3.skillEffect4;
                    setPetHoverArea(_loc_4, _loc_3.mouseArea4);
                    break;
                }
                case 5:
                {
                    _loc_4.bone = _loc_3.bone5;
                    _loc_4.texture = _loc_3.texture5;
                    _loc_4.mesh = _loc_3.mesh5;
                    _loc_4.effect = _loc_3.effect5;
                    _loc_4.skillEffect = _loc_3.skillEffect5;
                    setPetHoverArea(_loc_4, _loc_3.mouseArea5);
                    break;
                }
                default:
                {
                    _loc_4.bone = _loc_3.bone1;
                    _loc_4.texture = _loc_3.texture1;
                    _loc_4.mesh = _loc_3.mesh1;
                    _loc_4.effect = _loc_3.effect1;
                    setPetHoverArea(_loc_4, _loc_3.mouseArea1);
                    break;
                    break;
                }
            }
            return _loc_4;
        }// end function

        public static function setPetHoverArea(param1:EntityModelInfo, param2:String) : void
        {
            var _loc_3:Array = null;
            if (param2)
            {
                _loc_3 = param2.split("#");
                param1.hoverLeft = int(_loc_3[0]);
                param1.hoverTop = int(_loc_3[1]);
                param1.hoverWidth = int(_loc_3[2]);
                param1.hoverHeight = int(_loc_3[3]);
            }
            else
            {
                param1.hoverLeft = 40;
                param1.hoverTop = 100;
                param1.hoverWidth = 80;
                param1.hoverHeight = 120;
            }
            return;
        }// end function

        public static function petTotemCanComp(param1:int) : Boolean
        {
            var _loc_2:* = PetTotemConfig.instance.getEquipItem(param1);
            if (_loc_2 == null)
            {
                return false;
            }
            if (_loc_2.piece > 0)
            {
                return Cache.instance.pack.backPackCache.getDropItemCountByCode(_loc_2.piece) >= _loc_2.pieceNum;
            }
            if (_loc_2.material1 > 0 && (Cache.instance.pack.backPackCache.getDropItemCountByCode(_loc_2.material1) > 0 || petTotemCanComp(_loc_2.material1)))
            {
                if (_loc_2.material2 == 0 || _loc_2.material2 > 0 && (Cache.instance.pack.backPackCache.getDropItemCountByCode(_loc_2.material2) > 0 || petTotemCanComp(_loc_2.material2)))
                {
                    if (_loc_2.material3 == 0 || _loc_2.material3 > 0 && (Cache.instance.pack.backPackCache.getDropItemCountByCode(_loc_2.material3) > 0 || petTotemCanComp(_loc_2.material3)))
                    {
                        if (_loc_2.material4 == 0 || _loc_2.material4 > 0 && (Cache.instance.pack.backPackCache.getDropItemCountByCode(_loc_2.material4) > 0 || petTotemCanComp(_loc_2.material4)))
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }// end function

        public static function getTotemAttrName(param1:int) : Array
        {
            var _loc_3:String = null;
            var _loc_2:Array = [];
            var _loc_4:* = ATTRIBUTE_NAME_3.length;
            var _loc_5:* = PetTotemConfig.instance.getTargetItem(param1, 1);
            var _loc_6:int = 0;
            while (_loc_6 < _loc_4)
            {
                
                _loc_3 = PetUtil.ATTRIBUTE_NAME_3[_loc_6];
                if (_loc_5.hasOwnProperty(_loc_3) && _loc_5[_loc_3] > 0)
                {
                    _loc_2.push(_loc_3);
                }
                _loc_6++;
            }
            return _loc_2;
        }// end function

        public static function getScaling(param1:int) : Number
        {
            return (param1 == 0 ? (100) : (param1)) / 100;
        }// end function

        public static function getToTemEffect(param1:int, param2:int) : String
        {
            if (param1 == 1)
            {
                return "tuteng_" + param2 + "_bai";
            }
            if (param1 == 2)
            {
                return "tuteng_" + param2 + "_lv";
            }
            if (param1 == 3)
            {
                return "tuteng_" + param2 + "_lan";
            }
            if (param1 == 4)
            {
                return "tuteng_" + param2 + "_zi";
            }
            return "tuteng_" + param2 + "_cheng";
        }// end function

        public static function get maxPet() : SPet
        {
            var _loc_3:SPet = null;
            var _loc_1:* = Cache.instance.pet;
            var _loc_2:* = _loc_1.getSpetByUid(_loc_1.fightPetUid);
            if (_loc_2 == null)
            {
                for each (_loc_3 in _loc_1.pets)
                {
                    
                    if (_loc_2 == null || _loc_3.publicPet.level > _loc_2.publicPet.level || _loc_3.publicPet.level == _loc_2.publicPet.level && _loc_3.publicPet.experience > _loc_2.publicPet.experience)
                    {
                        _loc_2 = _loc_3;
                    }
                }
            }
            return _loc_2;
        }// end function

        public static function get avatarPet() : SPet
        {
            var _loc_1:* = Cache.instance.pet;
            var _loc_2:* = _loc_1.getSpetByUid(_loc_1.fightPetUid);
            if (_loc_2 == null)
            {
                _loc_2 = _loc_1.getSpetByUid(_loc_1.lastFightUid);
                if (_loc_2 == null)
                {
                    _loc_2 = PetUtil.maxPet;
                }
            }
            return _loc_2;
        }// end function

        public static function getSkillCellToolTips(param1:SPet) : String
        {
            var _loc_2:* = Language.getArrayByStr(70024);
            var _loc_3:* = JSON.parse(param1.publicPet.blood) as Array;
            var _loc_4:String = "";
            if (param1.publicPet.growthMax < PetConst.OPEN_SKILL_POS_5_REQUIRE_GROWTH)
            {
                _loc_4 = _loc_2[0];
            }
            if (param1.publicPet.growthMax < PetConst.OPEN_SKILL_POS_6_REQUIRE_GROWTH)
            {
                _loc_4 = _loc_4.length > 0 ? (_loc_4 + "\n" + _loc_2[1]) : (_loc_4 + _loc_2[1]);
            }
            if (param1.publicPet.growthMax < PetConst.OPEN_SKILL_POS_7_REQUIRE_GROWTH)
            {
                _loc_4 = _loc_4.length > 0 ? (_loc_4 + "\n" + _loc_2[2]) : (_loc_4 + _loc_2[2]);
            }
            if (param1.publicPet.growthMax < PetConst.OPEN_SKILL_POS_8_REQUIRE_GROWTH)
            {
                _loc_4 = _loc_4.length > 0 ? (_loc_4 + "\n" + _loc_2[3]) : (_loc_4 + _loc_2[3]);
            }
            return _loc_4;
        }// end function

        public static function get petSkillRandInfo() : ItemInfo
        {
            return ItemConfig.instance.getItemInfoByCondition(EGroup._EGroupProp, EProp._EPropProp, EPropType._EPropTypePetSkillRand, 1, 3);
        }// end function

        public static function getAttrValueByName(param1:SPet, param2:String) : int
        {
            return 0;
        }// end function

        public static function getPropNum() : int
        {
            var _loc_3:ItemData = null;
            var _loc_4:Array = null;
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            var _loc_1:* = Cache.instance.newPet.pet;
            if (_loc_1 == null)
            {
                return 0;
            }
            var _loc_2:* = MountConfig.instance.getFashionInfoByCode(EAdvanceComponentType._EAdvanceComponentTypePet, _loc_1.code);
            if (_loc_2)
            {
                _loc_3 = new ItemData(int(_loc_2.propConsume));
                _loc_4 = Cache.instance.pack.backPackCache.getItemsByGroupCategoryAndType(_loc_3.itemInfo.group, _loc_3.itemInfo.category, _loc_3.itemInfo.type);
                _loc_6 = _loc_4.length;
                while (_loc_7 < _loc_6)
                {
                    
                    if (ItemsUtil.isPrescriOutTime(_loc_4[_loc_7] as ItemData))
                    {
                    }
                    else
                    {
                        _loc_5 = _loc_5 + (_loc_4[_loc_7] as ItemData).itemAmount;
                    }
                    _loc_7++;
                }
                return _loc_5;
            }
            return 0;
        }// end function

        public static function get hasPetCanGrowing() : Boolean
        {
            var _loc_6:int = 0;
            var _loc_7:Array = null;
            var _loc_8:int = 0;
            var _loc_1:* = Cache.instance.newPet;
            if (isPetTopLevel())
            {
                return false;
            }
            var _loc_2:* = MountUtil.isHasBlessItem(EAdvanceType._EAdvanceTypePetGrowth);
            if (_loc_2)
            {
                return true;
            }
            var _loc_3:* = Cache.instance.newPet.pet;
            if (_loc_3 == null)
            {
                return false;
            }
            var _loc_4:* = MountConfig.instance.getFashionInfoByCode(EAdvanceComponentType._EAdvanceComponentTypePet, _loc_3.code);
            var _loc_5:* = getPropNum();
            if (_loc_4.blessClear)
            {
                if (Cache.instance.newPet.pet.bless)
                {
                    return true;
                }
                _loc_7 = _loc_4.advance.split("&");
                _loc_8 = (_loc_7[(_loc_7.length - 1)] as String).split("#")[0];
                _loc_6 = (_loc_8 - _loc_3.advanceNum) * _loc_4.propNum;
            }
            else
            {
                _loc_6 = _loc_4.propNum;
            }
            return _loc_5 >= _loc_6;
        }// end function

        public static function getPetCfg(param1:int = -1) : TMountConfig
        {
            var _loc_3:SPublicPet2 = null;
            if (param1 == -1)
            {
                _loc_3 = Cache.instance.newPet.pet;
                param1 = _loc_3.code;
            }
            var _loc_2:* = MountConfig.instance.getFashionInfoByCode(EAdvanceComponentType._EAdvanceComponentTypePet, param1);
            return _loc_2;
        }// end function

        public static function isPetTopLevel() : Boolean
        {
            var _loc_1:* = Cache.instance.newPet.pet;
            if (_loc_1 == null)
            {
                return false;
            }
            var _loc_2:* = MountConfig.instance.getFashionInfoByCode(EAdvanceComponentType._EAdvanceComponentTypePet, (_loc_1.code + 1));
            if (_loc_2)
            {
                return false;
            }
            return true;
        }// end function

        public static function petScaleValue(param1:int) : Number
        {
            if (param1 == 0)
            {
                return 1;
            }
            return param1 / 100;
        }// end function

        public static function petAttrByName(param1:String, param2:TMountConfig) : int
        {
            var _loc_9:ItemData = null;
            var _loc_3:* = Cache.instance.newPet.pet;
            var _loc_4:* = PlayerTargetConfig.instance.getInfoById(_mountGroupItem.itemInfo.effectEx);
            var _loc_5:* = Cache.instance.pack.petBag.allItems;
            var _loc_6:int = 0;
            var _loc_7:* = Cache.instance.vip.level;
            var _loc_8:* = VipConfig.instance.getVipConfigByType(EVipRecordType._EVipRecordTypePetEffect);
            _loc_6 = _loc_6 + param2[param1];
            for each (_loc_9 in _loc_5)
            {
                
                if (_loc_9)
                {
                    _loc_6 = _loc_6 + _loc_9.itemInfo[param1];
                }
            }
            if (_loc_3.totalGrowthNum > 0 && _loc_4 && _loc_4.hasOwnProperty(param1))
            {
                _loc_6 = _loc_6 + _loc_4[param1] * _loc_3.totalGrowthNum;
            }
            if (_loc_3.totalTalentNum > 0)
            {
                _loc_6 = _loc_6 + param2[param1] * _mountQualifiedItem.itemInfo.effectEx * 0.01 * _loc_3.totalTalentNum;
            }
            if (_loc_7 > 0)
            {
                _loc_6 = _loc_6 + param2[param1] * int(_loc_8.desc) * int(_loc_8["vip" + _loc_7]) * 0.01;
            }
            return int(_loc_6);
        }// end function

        public static function getPetEquipCombatByItemData(param1:ItemData) : int
        {
            var _loc_4:String = null;
            var _loc_2:* = MountConfig.instance.attribute;
            var _loc_3:Number = 0;
            for each (_loc_4 in _loc_2)
            {
                
                _loc_3 = _loc_3 + CombatUtil.getCombatValueByName(_loc_4, param1.itemInfo[_loc_4]);
            }
            return int(_loc_3);
        }// end function

        public static function isEnoughLevel(param1:ItemData) : Boolean
        {
            if (!Cache.instance.newPet.pet)
            {
                return false;
            }
            return Boolean(param1.itemInfo.level <= Cache.instance.newPet.pet.code);
        }// end function

        public static function petCombat(param1:TMountConfig) : int
        {
            var _loc_9:String = null;
            var _loc_10:Array = null;
            var _loc_11:Dictionary = null;
            var _loc_12:SkillInfo = null;
            var _loc_13:SkillInfo = null;
            var _loc_14:int = 0;
            var _loc_15:Number = NaN;
            var _loc_16:ItemData = null;
            var _loc_2:* = MountConfig.instance.attribute;
            var _loc_3:* = Cache.instance.newPet.pet;
            var _loc_4:* = PlayerTargetConfig.instance.getInfoById(_mountGroupItem.itemInfo.effectEx);
            var _loc_5:* = Cache.instance.pack.petBag.allItems;
            var _loc_6:Number = 0;
            var _loc_7:* = Cache.instance.vip.level;
            var _loc_8:* = VipConfig.instance.getVipConfigByType(EVipRecordType._EVipRecordTypePetEffect);
            for each (_loc_9 in _loc_2)
            {
                
                _loc_15 = 0;
                _loc_15 = _loc_15 + param1[_loc_9];
                for each (_loc_16 in _loc_5)
                {
                    
                    if (_loc_16)
                    {
                        _loc_15 = _loc_15 + _loc_16.itemInfo[_loc_9];
                    }
                }
                if (_loc_3.totalGrowthNum > 0 && _loc_4.hasOwnProperty(_loc_9))
                {
                    _loc_15 = _loc_15 + _loc_4[_loc_9] * _loc_3.totalGrowthNum;
                }
                if (_loc_3.totalTalentNum > 0)
                {
                    _loc_15 = _loc_15 + param1[_loc_9] * _mountQualifiedItem.itemInfo.effectEx * 0.01 * _loc_3.totalTalentNum;
                }
                if (_loc_7 > 0)
                {
                    _loc_15 = _loc_15 + param1[_loc_9] * int(_loc_8.desc) * int(_loc_8["vip" + _loc_7]) * 0.01;
                }
                _loc_15 = CombatUtil.getCombatValueByName(_loc_9, int(_loc_15));
                _loc_6 = _loc_6 + _loc_15;
            }
            _loc_10 = getPetLearnSkill();
            _loc_11 = new Dictionary();
            _loc_14 = 0;
            while (_loc_14 < _loc_10.length)
            {
                
                _loc_12 = _loc_10[_loc_14];
                _loc_13 = _loc_11[_loc_12.tSkill.posType] as SkillInfo;
                if (_loc_13 == null || _loc_13 && _loc_12.tSkill.skillLevel > _loc_13.tSkill.skillLevel)
                {
                    _loc_11[_loc_12.tSkill.posType] = _loc_12;
                }
                _loc_14++;
            }
            for each (_loc_12 in _loc_11)
            {
                
                _loc_6 = _loc_6 + _loc_12.tSkill.combat;
            }
            return int(_loc_6);
        }// end function

        public static function getPetLearnSkill() : Array
        {
            var _loc_4:SkillInfo = null;
            var _loc_1:Array = [];
            var _loc_2:* = Cache.instance.newPet.pet;
            if (_loc_2 == null)
            {
                return [];
            }
            var _loc_3:int = 0;
            _loc_3 = 0;
            while (_loc_3 < 8)
            {
                
                if (_loc_2.code >= (_loc_3 + 1))
                {
                    _loc_4 = Cache.instance.newPet.getPetSkill(_loc_3 + PetConst.PASSIVE_SKILL_START_POS);
                    if (_loc_4)
                    {
                        _loc_1.push(_loc_4);
                    }
                }
                _loc_3++;
            }
            return _loc_1;
        }// end function

        public static function get nextPetAddCombat() : int
        {
            var _loc_1:* = Cache.instance.newPet.pet;
            if (_loc_1.code >= maxPetLevel)
            {
                return 0;
            }
            var _loc_2:* = MountConfig.instance.getFashionInfoByCode(EAdvanceComponentType._EAdvanceComponentTypePet, _loc_1.code);
            var _loc_3:* = MountConfig.instance.getFashionInfoByCode(EAdvanceComponentType._EAdvanceComponentTypePet, (_loc_1.code + 1));
            return petCombat(_loc_3) - petCombat(_loc_2);
        }// end function

    }
}
